package com.up.cloudfeiyibackend.controller;

import com.up.cloudfeiyibackend.result.Result;
import com.up.cloudfeiyibackend.vo.EventSignupStatusVO;


import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * 通用接口
 */
@RestController
@RequestMapping("/comment")
public class CommentUplodController {

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 上传文件
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //日期路径
            String datePath = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")) + "/";
            //文件后缀名
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //新文件名
            String uniqueFileName = UUID.randomUUID() + extension;

            //最终上传文件路径
            String finalFilePath = datePath + uniqueFileName;
            //上传文件
            FileInfo fileInfo = fileStorageService.of(file)
                    .setPath(datePath)
                    .setSaveFilename(uniqueFileName)
                    .upload();

            return Result.success("上传成功",fileInfo.getUrl());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传失败");
        }

    }
}
